{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "def19bcb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0., 0., 0., 2., 2., 2., 2., 0.])\n",
      "tensor([1., 1., 1., 1., 1., 1., 1., 1.])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "drop = nn.Dropout()\n",
    "x = torch.ones(8)\n",
    "\n",
    "# Train mode   \n",
    "drop.train()\n",
    "print(drop(x)) # tensor([0., 0., 0., 0., 2., 0., 0., 0.])\n",
    "\n",
    "# Eval mode   \n",
    "drop.eval()\n",
    "print(drop(x)) # tensor([1., 1., 1., 1., 1., 1., 1., 1.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14abf5fe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d2299e60",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'num_embeddings' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [10]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mEmbedding(\u001b[43mnum_embeddings\u001b[49m, embedding_dim, padding_idx\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, max_norm\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, norm_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2.0\u001b[39m, scale_grad_by_freq\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, sparse\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, _weight\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'num_embeddings' is not defined"
     ]
    }
   ],
   "source": [
    "torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False, _weight=None)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "ff7bbfce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[[-0.4667,  1.4833,  0.3891],\n",
       "          [ 0.2661, -1.6009, -0.0770],\n",
       "          [ 1.6082, -1.0167, -0.3010],\n",
       "          [ 0.1874, -1.1285, -0.7755]],\n",
       " \n",
       "         [[ 1.6082, -1.0167, -0.3010],\n",
       "          [-0.5914,  1.2128, -0.1656],\n",
       "          [ 0.2661, -1.6009, -0.0770],\n",
       "          [ 2.2954, -0.0690,  1.3059]]], grad_fn=<EmbeddingBackward0>),\n",
       " torch.Size([2, 4, 3]))"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# an Embedding module containing 10 tensors of size 3\n",
    "embedding = nn.Embedding(10, 3)\n",
    "# a batch of 2 samples of 4 indices each\n",
    "input = torch.LongTensor([[1,2,4,0],[4,3,2,9]])\n",
    "embedding(input),embedding(input).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "36a5d2fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Parameter containing:\n",
       "tensor([[ 0.1874, -1.1285, -0.7755],\n",
       "        [-0.4667,  1.4833,  0.3891],\n",
       "        [ 0.2661, -1.6009, -0.0770],\n",
       "        [-0.5914,  1.2128, -0.1656],\n",
       "        [ 1.6082, -1.0167, -0.3010],\n",
       "        [ 0.7409,  0.0977,  0.4372],\n",
       "        [-0.0769,  0.0574, -0.5954],\n",
       "        [-0.6033, -0.2272, -1.0853],\n",
       "        [-0.6692,  0.6012, -0.4271],\n",
       "        [ 2.2954, -0.0690,  1.3059]], requires_grad=True)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embedding.weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caf5e28f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10f39e5d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "a2a0557c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-0.5599, -1.5424, -0.2132],\n",
       "         [ 0.2835,  0.4542,  0.2967],\n",
       "         [-0.5165, -0.9710, -0.1075],\n",
       "         [ 0.0000,  0.0000,  0.0000]],\n",
       "\n",
       "        [[-0.5165, -0.9710, -0.1075],\n",
       "         [-0.9404,  0.9598,  0.5672],\n",
       "         [ 0.2835,  0.4542,  0.2967],\n",
       "         [ 1.0694, -0.6270,  0.3150]]], grad_fn=<EmbeddingBackward0>)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# example with padding_idx\n",
    "embedding = nn.Embedding(10, 3, padding_idx=0)\n",
    "input = torch.LongTensor([[1,2,4,0],[4,3,2,9]])\n",
    "embedding(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "1c864b75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Parameter containing:\n",
       "tensor([[ 0.0000,  0.0000,  0.0000],\n",
       "        [-0.5599, -1.5424, -0.2132],\n",
       "        [ 0.2835,  0.4542,  0.2967],\n",
       "        [-0.9404,  0.9598,  0.5672],\n",
       "        [-0.5165, -0.9710, -0.1075],\n",
       "        [-1.1953, -1.1011,  0.0668],\n",
       "        [-0.0070, -0.4395, -1.8729],\n",
       "        [ 0.0693,  0.6212,  0.5721],\n",
       "        [ 0.6246,  0.7526, -1.0420],\n",
       "        [ 1.0694, -0.6270,  0.3150]], requires_grad=True)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embedding.weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bd51a01",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
